public class MinEatingSpeed {

    public int minEatingSpeed(int[] piles, int h) {
        int l = 1;
        int r = Integer.MIN_VALUE;
        for (int i = 0; i < piles.length; i++) {
            r = Math.max(r, piles[i]);
        }
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (process(piles, mid, h)) {
                r = mid;
            } else l = mid + 1;
        }
        return l;
    }

    public boolean process(int[] piles, int target, int h) {
        int ans = 0;
        for (int i = 0; i < piles.length; i++) {
            ans += piles[i] % target == 0 ? piles[i] / target : piles[i] / target + 1;
        }
        return ans <= h;
    }
}
